﻿using Engine;
using Game;
using Game.NetWork;
using Game.NetWork.Packages;
using Game.Server;
using Game.Server.Event;
using Game.Server.PlayerEvent;
using GameEntitySystem;
using MessagePack;
using MessagePack.Formatters;
using Microsoft.SqlServer.Server;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using XmlUtilities;

namespace Game.Server.Plugins
{
    public class 离线玩家记录插件 : ServerPlugin, IPlayerEnterGameEventHandle, ICreatureSpawnEventHandle
    {
        public override int Version => 100;

        public class 玩家状态
        {
            public string name;
            public bool 在线状态;
        }

        public static Dictionary<Guid, 玩家状态> 内存字典 = new Dictionary<Guid, 玩家状态>();

        public readonly string path = "离线玩家.json";

        public override string Name => "离线玩家记录插件";

        public byte m_firstLevel = 1;

        public byte FirstLevel => m_firstLevel;

        public SubsystemPlayers m_SubsystemPlayers;

        public SubsystemTime m_subsystemTime;

        public override void Initialize()
        {

            CreatureSpawnEventManager.AddObject(this);
            PlayerEnterGameEventManager.AddObject(this);

            if (!File.Exists(path))
            {
                Dictionary<Guid, 玩家状态> 初始字典 = new Dictionary<Guid, 玩家状态>()
                {
                    { new Guid("11111111-1111-1111-1111-111111111111"),new 玩家状态{name = "r",在线状态 = true } }
                };
                File.Create(path).Close();
                File.WriteAllText(path, JsonConvert.SerializeObject(初始字典));// 写入json文件
                Log.Information("文件创建成功");
            }
            else
            {
                Log.Information("已创建");
            }
        }

        public void 写入文件()
        {
            File.WriteAllText(path, JsonConvert.SerializeObject(内存字典)); // 写入json文件
        }

        public override void Load()
        {
            m_subsystemTime = GameManager.Project.FindSubsystem<SubsystemTime>();
            读取();
        }

        public void 读取()
        {
            m_SubsystemPlayers = GameManager.Project.FindSubsystem<SubsystemPlayers>();
            string path1 = File.ReadAllText(path);
            内存字典 = JsonConvert.DeserializeObject<Dictionary<Guid, 玩家状态>>(path1);
        }

        public override void Save()
        {
            写入文件();
        }

        public void PlayerEnter(PlayerData playerData)
        {

            if (内存字典.ContainsKey(playerData.PlayerGUID))
            {
                内存字典[playerData.PlayerGUID].name = playerData.Name;
                内存字典[playerData.PlayerGUID].在线状态 = true;
            }
            else
            {
                内存字典.Add(playerData.PlayerGUID, new 玩家状态 { name = playerData.Name, 在线状态 = true });
            }

        }

        public void PlayerLeave(PlayerData playerData)
        {

            if (内存字典.ContainsKey(playerData.PlayerGUID))
            {
                内存字典[playerData.PlayerGUID].name = playerData.Name;
                内存字典[playerData.PlayerGUID].在线状态 = false;
            }
            else
            {
                内存字典.Add(playerData.PlayerGUID, new 玩家状态 { name = playerData.Name, 在线状态 = false });
            }
        }

        public bool Update(SubsystemCreatureSpawn subsystemCreatureSpawn, float dt)
        {
            if (m_subsystemTime.PeriodicGameTimeEvent(10.0, 0.0))
            {
                foreach (var playerData in m_SubsystemPlayers.PlayersData)
                {
                    if (内存字典.ContainsKey(playerData.PlayerGUID) && 内存字典[playerData.PlayerGUID].name != playerData.m_name)
                    {
                        内存字典[playerData.PlayerGUID].name = playerData.m_name;
                        Log.Information($"玩家 {playerData.PlayerGUID} 改名: {内存字典[playerData.PlayerGUID].name} -> {playerData.m_name}");
                        写入文件(); 读取();
                    }
                }
            }

            return true;
        }

        public void OnEntityAdded(SubsystemCreatureSpawn subsystemCreatureSpawn, Entity entity)
        {

        }

        public void OnEntityRemoved(SubsystemCreatureSpawn subsystemCreatureSpawn, Entity entity)
        {

        }

        public void OnPlayerSpawned(PlayerData playerData, Entity playerEntity, Vector3 position)
        {

        }

        public void InitCreatureTypes(SubsystemCreatureSpawn subsystemCreatureSpawn, List<SubsystemCreatureSpawn.CreatureType> creatureTypes)
        {

        }
    }

    public class dadawdadadadad : AbstractProcessCmd
    {
        public override string Cmd => "ff";

        public override string Introduce => "ff on";

        public override int AuthLevel => 1;

        public override DisplayType Display => DisplayType.All;

        public override void ProcessCmd()
        {
            ComponentPlayer componentPlayer = m_client.PlayerData.ComponentPlayer;
            if (GameManager.Project == null) return;
            string hdsa = m_messageDatas[1]; // 静态变量赋值
            if (hdsa == "on")
            {
                foreach (var player in 离线玩家记录插件.内存字典)
                {
                    string 在线状态 = player.Value.在线状态 ? "在线" : "离线";
                    string Guid = player.Key.ToString();
                    string 名称 = player.Value.name;

                    SendMsgPack(componentPlayer, 在线状态 + " " + Guid + " " + 名称);
                }

            }

        }

        public void SendMsgPack(ComponentPlayer componentPlayer, string text)  //发送消息方法
        {
            MessagePackage messagePackage = new MessagePackage(null, text, 0, null);
            messagePackage.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackage);
        }

    }

}